package com.haohan;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haohan.dao.ServerInfoDao;
import com.haohan.domain.ServerInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Arrays;

@SpringBootTest
public class BookDaoTestCase {

    @Autowired
    private ServerInfoDao serverInfoDao;

    @Test
    void testGetById(){
        System.out.println(serverInfoDao.selectById(2));
    }

    @Test
    void testSave(){
        ServerInfo serverInfo = new ServerInfo();
        serverInfo.setServerIp("10.10.4.44");
        serverInfo.setUsername("root");
        serverInfo.setPassword("1qaz2wsx");
        serverInfo.setRack("A01");
        serverInfo.setDepartment("应用产品部");
        serverInfoDao.insert(serverInfo);
    }

    /**
     * 批量存储数据
     */
    @Test
    void testSaveBatch(){
/*        ServerInfo serverInfo1 = new ServerInfo();
        serverInfo1.setServerIp("10.10.4.41");
        serverInfo1.setUsername("root");
        serverInfo1.setPassword("tma@1100");
        serverInfo1.setRack("A02");
        serverInfo1.setDepartment("应用产品部");
        ServerInfo serverInfo2 = new ServerInfo();
        serverInfo2.setServerIp("10.10.4.51");
        serverInfo2.setUsername("root");
        serverInfo2.setPassword("tma1100!@jszx");
        serverInfo2.setRack("A01");
        serverInfo2.setDepartment("应用产品部");*/
        ServerInfo serverInfo1 = new ServerInfo();
        serverInfo1.setServerIp("10.10.3.45");
        serverInfo1.setUsername("root");
        serverInfo1.setPassword("tma@1100");
        serverInfo1.setRack("A03");
        serverInfo1.setDepartment("应用产品部");
        ServerInfo serverInfo2 = new ServerInfo();
        serverInfo2.setServerIp("10.10.3.35");
        serverInfo2.setUsername("root");
        serverInfo2.setPassword("tma1100!@jszx");
        serverInfo2.setRack("A03");
        serverInfo2.setDepartment("应用产品部");
        serverInfoDao.saveBatch(Arrays.asList(serverInfo1,serverInfo2));
    }

    @Test
    void testUpdate(){
        ServerInfo serverInfo = new ServerInfo();
        serverInfo.setId(3);
        serverInfo.setBmcIp("10.10.4.58");
        serverInfo.setBmcUser("haohan");
        serverInfo.setBmcPass("tma@1100");
        serverInfoDao.updateById(serverInfo);
    }

    @Test
    void testDeleteBatch(){
        serverInfoDao.deleteBatchIds(Arrays.asList(4,5));
    }

    @Test
    void getAll(){
        System.out.println(serverInfoDao.selectList(null));
    }

    @Test
    void testGetPage(){
        IPage page = new Page(2,2);
        serverInfoDao.selectPage(page, null);
        System.out.println(page.getCurrent());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.getPages());
    }

    @Test
    void testGetBy(){
        QueryWrapper<ServerInfo> qw = new QueryWrapper<>();
        qw.like("server_ip","10.10.4.");
        serverInfoDao.selectList(qw);
    }

    @Test
    void testLambda() {
        LambdaQueryWrapper<ServerInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ServerInfo::getServerIp,"10.10.4.41");
        serverInfoDao.selectList(lqw);
    }
/*
    @Test
    void testDelete(){
        bookDao.deleteById(16);
    }

    @Test
    void testGetAll(){
        bookDao.selectList(null);
    }

    @Test
    void testGetPage(){
        IPage page = new Page(2,5);
        bookDao.selectPage(page, null);
        System.out.println(page.getCurrent());
        System.out.println(page.getSize());
        System.out.println(page.getTotal());
        System.out.println(page.getPages());
        System.out.println(page.getRecords());
    }

    @Test
    void testGetBy(){
        QueryWrapper<Book> qw = new QueryWrapper<>();
        qw.like("name","Spring");
        bookDao.selectList(qw);
    }

    @Test
    void testGetBy2(){
        String name = "1";
        LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
        //if(name != null) lqw.like(Book::getName,name);
        lqw.like(name != null,Book::getName,name);
        bookDao.selectList(lqw);
    }*/

}










